package session.stateless.FRM;

import entity.FRM.CashInAdvance;
import entity.CRM.Customer;
import entity.CRM.SaleOrder;
import entity.FRM.CreditAccount;
import entity.FRM.CustomerPayment;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class CashInAdvanceBean implements CashInAdvanceBeanLocal 
{
    @PersistenceContext 
    private EntityManager em; 
    
    @EJB
    CreditAccountBeanLocal creditAccountBeanLocal;
    
    @Override
    public Long createCashInAdvance_Pending(Long saleOrderId, Double cashInAdvanceAmount) {
        SaleOrder saleOrder = em.find(SaleOrder.class, saleOrderId);
        Customer customer = saleOrder.getCustomer();
        CreditAccount creditAccount = customer.getCreditAccount();
        
        CashInAdvance cashInAdvance = new CashInAdvance();
        cashInAdvance.setCashInAdvanceAmount(cashInAdvanceAmount);
        cashInAdvance.setStateCashInAdvance(CashInAdvance.StateCashInAdvance.Pending);
        em.persist(cashInAdvance);
        
        saleOrder.setCashInAdvance(cashInAdvance);
        creditAccount.getCashInAdvances().add(cashInAdvance);
        em.flush();
        
        return cashInAdvance.getCashInAdvanceId();
    }
    public void createCashInAdvance(Long customerPaymentId) {
        CustomerPayment customerPayment = em.find(CustomerPayment.class, customerPaymentId);
        SaleOrder saleOrder = customerPayment.getCustomerInvoice().getSaleOrder();
        
        CashInAdvance cashInAdvance = new CashInAdvance();
        cashInAdvance.setCashInAdvanceAmount(customerPayment.getCustomerPaymentAmount());
        em.persist(cashInAdvance);
        
        Customer customer = saleOrder.getCustomer();
        CreditAccount creditAccount = customer.getCreditAccount();
        
        creditAccount.getCashInAdvances().add(cashInAdvance);
        saleOrder.setCashInAdvance(cashInAdvance);
        
        em.flush();
    }

    public void editCashInAdvance(Long saleOrderId, Double newAmount) {
        SaleOrder saleOrder= em.find(SaleOrder.class, saleOrderId);
        Customer customer = saleOrder.getCustomer();
        Double oldAmount = saleOrder.getCashInAdvance().getCashInAdvanceAmount();
        
        saleOrder.getCashInAdvance().setCashInAdvanceAmount(newAmount);
        customer.getCreditAccount().setOutstandingCredit(customer.getCreditAccount().getOutstandingCredit()+oldAmount-newAmount);
        em.flush();
    }
    
//    public void deleteCashInAdvance(Long saleOrderId) //Actually, this method is just said the amount of cash in advance to be 0 instead of removing it in the database
//    {
//        SaleOrder saleOrder = em.find(SaleOrder.class, saleOrderId);
//        CreditAccount creditAccount = saleOrder.getCustomer().getCreditAccount();
//        CashInAdvance cashInAdvance = saleOrder.getCashInAdvance();
//        
//        creditAccountBeanLocal.modifyCreditBalance_RefundCashInAdvance(saleOrder.getId(), saleOrder.getCashInAdvance().getCashInAdvanceAmount());
//        
//        cashInAdvance.setCashInAdvanceAmount(new Double(0));
//        em.flush();
//    }

    @Override
    public CashInAdvance findCashInAdvanceById(Long cashInAdvanceId) {
        CashInAdvance temp = em.find(CashInAdvance.class, cashInAdvanceId);
        return temp;
    }

    
}
